package us.icitap.timsportal.management;

import java.util.Arrays;
import java.util.List;

import us.icitap.timsportal.entity.Officer;
import us.icitap.timsportal.resources.TimsPortalLabels;
import us.icitap.timsportal.web.custom.PNativeButton;
import us.icitap.timsportal.web.custom.PortalForm;
import us.icitap.timsportal.web.custom.PortalGridLayout;
import us.icitap.timsportal.web.custom.PortalLabel;
import us.icitap.timsportal.web.custom.PTextField;

import com.vaadin.data.Item;
import com.vaadin.data.util.BeanItem;
import com.vaadin.terminal.Sizeable;
import com.vaadin.ui.Component;
import com.vaadin.ui.DefaultFieldFactory;
import com.vaadin.ui.Field;

public class OfficerForm extends PortalForm {

	private static final long serialVersionUID = 1L;

	private static final Object[] NATURAL_COL_ORDER = new Object[] {
		"officerId", "forename", "rank", "function", "orgStructure" };
	private List<Object> fieldNames = Arrays.asList(NATURAL_COL_ORDER);
	
	private PortalGridLayout gLayout = new PortalGridLayout(4, 5, true, "light");
	
	PNativeButton search = new PNativeButton(TimsPortalLabels.getLabel("btn.search"));
	PNativeButton clear = new PNativeButton(TimsPortalLabels.getLabel("btn.clear"));
	
	PTextField officerId = new PTextField();
	private PTextField forename = new PTextField();
	private PTextField rank = new PTextField();
	private PTextField function = new PTextField();
	private PTextField orgStructure = new PTextField();
	
	private Officer officer;
	
	public OfficerForm(){
		setWriteThrough(false);
		gLayout.setStyleName("light-blue");
		setCaption(TimsPortalLabels.getLabel("title.officerForm"));
		setLayout(gLayout);
		setWidth(Sizeable.SIZE_UNDEFINED, 0);
		search.setWidth("100px");
		clear.setWidth("100px");

		gLayout.setMargin(false, false, false, false);
		gLayout.addComponent(
				new PortalLabel(TimsPortalLabels.getLabel("label.officerId")), 0, 0);
		gLayout.addComponent(
				new PortalLabel(TimsPortalLabels.getLabel("label.fullName")), 0, 1);
		gLayout.addComponent(
				new PortalLabel(TimsPortalLabels.getLabel("label.rank")), 0, 2);
		gLayout.addComponent(
				new PortalLabel(TimsPortalLabels.getLabel("label.function")), 0, 3);
		gLayout.addComponent(
				new PortalLabel(TimsPortalLabels.getLabel("label.orgStructure")), 0, 4);
		gLayout.addComponent(search, 2, 0);
		gLayout.addComponent(clear, 3, 0);
		
		setFormFieldFactory(new DefaultFieldFactory(){

			private static final long serialVersionUID = 1L;

			public Field createField(Item item, Object propertyId,
					Component uiContext){
				
				Field field = super.createField(item, propertyId, uiContext);
				if(propertyId.equals("officerId")){
					officerId.setWidth("150px");
					return officerId;
				} else if(propertyId.equals("forename")){
					forename.setWidth("200px");
					return forename;
				} else if(propertyId.equals("rank")){
					rank.setWidth("200px");
					return rank;
				} else if(propertyId.equals("function")){
					function.setWidth("300px");
					return function;
				} else if(propertyId.equals("orgStructure")){
					orgStructure.setWidth("300px");
					return orgStructure;
				}				
				
				return field;				
			}
			
		});
	}
	
	@Override
	protected void attachField(Object propertyId, Field field){
		if(propertyId.equals("officerId"))
			gLayout.addComponent(field, 1, 0);
		else if(propertyId.equals("forename"))
			gLayout.addComponent(field, 1, 1, 3, 1);
		else if(propertyId.equals("rank"))
			gLayout.addComponent(field, 1, 2, 3, 2);
		else if(propertyId.equals("function"))
			gLayout.addComponent(field, 1, 3, 3, 3);
		else if(propertyId.equals("orgStructure"))
			gLayout.addComponent(field, 1, 4, 3, 4);
	}
	
	public void setOfficer(Officer officer){
		this.officer = officer;
		
		setReadOnly(false);
		if(officer!=null)
			this.setItemDataSource(new BeanItem<Officer>(this.officer), fieldNames);
		else this.setItemDataSource(new BeanItem<Officer>(new Officer()));
		setFormReadOnly(true);
		
	}
	
	public Officer getOfficer(){
		commit();
		return officer;
	}
	
	private void setFormReadOnly(Boolean readOnly){
		forename.setReadOnly(readOnly);
		rank.setReadOnly(readOnly);
		function.setReadOnly(readOnly);
		orgStructure.setReadOnly(readOnly);		
	}
	
	@Override
	public void attach(){
		super.attach();
		
	}

}
